﻿using AutoMapper;
using Dm;
using ERPPublic.eneity;
using FinancialManagement.Application.Command.SaleContractCommand;
using FinancialManagement.Domain.Dto.SaleContract;
using FinancialManagement.Domain.Model;
using FinancialManagement.Dtos;
using FinancialManagement.Infrstructre.Interface;
using Google.Protobuf.Collections;
using MediatR;
using SqlSugar;

namespace FinancialManagement.Application.CommandHandler.SaleContractCommandHandlers
{
    public class SaleContractBlikCommandHandlers : IRequestHandler<SaleContractBlikCommand, SaleContractShowDto>
    {
        IMapper mapper;
        private readonly ISaleContractProductRepository SRep;
        private readonly ISaleContractRepository CRep;
        ISqlSugarClient db;

        public SaleContractBlikCommandHandlers(IMapper mapper, ISaleContractProductRepository SRep, ISaleContractRepository CRep, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.SRep = SRep;
            this.CRep = CRep;
            this.db = db;
        }
        public async Task<SaleContractShowDto> Handle(SaleContractBlikCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await CRep.GetAllAsyns() select a).Where(x => x.IsDel == false&&x.SaleContractId==request.SaleContractId).ToList();
            var item = (from b in await SRep.GetAllAsyns() select b).ToList();
            var result = (from c in list
                          select new SaleContractShowDto
                          {
                              SaleContractId=c.SaleContractId,
                              ClientId=c.ClientId,
                              ClientName=c.ClientName,
                              SCAgencyDate=c.SCAgencyDate,
                              ContractTopic=c.ContractTopic,
                              SCcontractNumber=c.SCcontractNumber,
                              SCStartDate=c.SCStartDate,
                              SCEndDate=c.SCEndDate,
                              SCPeople=c.SCPeople,
                              SCCountpartPeople=c.SCCountpartPeople,
                              SCContractType=c.SCContractType,
                              SCSalespeopleId= c.SCSalespeopleId,
                              SCSalespeople=c.SCSalespeople,
                              DocumentMakers=c.DocumentMakers,
                              DocumentTime=c.DocumentTime,
                              SCcontractPrice=c.SCcontractPrice,
                              SCPreferentialPrice=c.SCPreferentialPrice,
                              PreferentialPrice=c.PreferentialPrice,
                              SundryPrice=c.SundryPrice,
                              SCReceivPeople=c.SCReceivPeople,
                              SCRePhone=c.SCRePhone,
                              ProvinceId=c.ProvinceId,
                              CityId=c.CityId,
                              SCAddress=c.SCAddress,
                              SCNum=c.SCNum,
                              SCTax=c.SCTax,
                              PreProductionId=c.PreProductionId,
                              RelevancyId=c.RelevancyId,
                              RelevancyIName=c.RelevancyIName,
                              SCOutState=c.SCOutState,
                              SCShipmentState=c.SCShipmentState,
                              AddName = c.AddName,
                              AddTime = c.AddTime,
                              UpdName = c.UpdName,
                              UpdTime = c.UpdTime,
                              DelName = c.DelName,
                              DelTime = c.DelTime,
                              IsDel = c.IsDel,
                              Office_Id = c.Office_Id,
                              User_Id = c.User_Id,
                              SaleContractProductDto = (from d in item
                                                       where d.SCcontractNumber==c.SCcontractNumber
                                                       select new SaleContractProducts
                                                       {
                                                         SaleContractProductId=d.SaleContractProductId,
                                                           SCcontractNumber=d.SCcontractNumber,
                                                           ProductId=d.ProductId,
                                                           PName=d.PName,
                                                           PNumber=d.PNumber,
                                                           SCPNum=d.SCPNum,
                                                           TaxPrice=d.TaxPrice,
                                                           Discount=d.Discount,
                                                           BillType=d.BillType,
                                                           DetailsPreferential=d.DetailsPreferential,
                                                           TaxLines=d.TaxLines,
                                                           DeliveryTime=d.DeliveryTime,
                                                           SRemark=d.SRemark,
                                                           SInventory=d.SInventory,
                                                           AddName=d.AddName,
                                                           AddTime=d.AddTime,
                                                           UpdName=d.UpdName,
                                                           UpdTime=d.UpdTime,
                                                           DelName=d.DelName,
                                                           DelTime=d.DelTime,
                                                           IsDel=d.IsDel,
                                                           Office_Id=d.Office_Id,
                                                           User_Id=d.User_Id,

                                                       }).ToList()


                          });
            return result.ToList().FirstOrDefault();
        }
    }
}
